package com.zzz.demo;

import com.zzz.demo.event.ContentModifyEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import java.util.HashMap;

@SpringBootApplication
@ServletComponentScan(value= "com.zzz.demo.listener")
public class TestDemoApplication {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(TestDemoApplication.class);

    public static void main(String[] args) {
//        SpringApplication.run(TestDemoApplication.class, args);
        SpringApplication build = new SpringApplicationBuilder()
                .sources(TestDemoApplication.class)
                .properties(new HashMap<String, Object>() {{
                    put("server.port", 0);
                }})
                .build();
        build.addListeners((ApplicationListener<ApplicationEvent>) applicationEvent -> {
            System.err.println(applicationEvent.toString());
            if (applicationEvent instanceof ContentModifyEvent) {
                String message = ((ContentModifyEvent) applicationEvent).getMessage();
                if (logger.isInfoEnabled()) {
                    logger.info("收到消息:{}",message);
                }
            }
        });
        build.setWebApplicationType(WebApplicationType.SERVLET);
        build.run(args);
    }

}
